package com.linqi.freebie.http

import com.linqi.freebie.http.ktorClientCio.HttpClientManager
import com.linqi.freebie.http.ktorClientCio.interceptor.BaseUrlInterceptor
import com.linqi.freebie.http.ktorClientCio.interceptor.HeaderInterceptor
import com.linqi.freebie.http.ktorClientCio.interceptor.LogInterceptor
import com.linqi.freebie.http.ktorClientCio.request.HttpRequestHelper
import com.linqi.freebie.config.Constants.GITHUB_API_BASE
import com.linqi.freebie.config.githubToken
import java.lang.reflect.Type

class DoHttp private constructor() {

    companion object {
        @Volatile
        private var instance: DoHttp? = null

        @JvmStatic
        fun getInstance() =
            instance ?: synchronized(this) {
                instance ?: DoHttp().also { instance = it }
            }
    }

    /**
     * 初始化HttpClientManager，添加默认拦截器
     */

    val token = githubToken

    fun init() {
        HttpClientManager
            .addInterceptor(BaseUrlInterceptor {GITHUB_API_BASE})
            .addInterceptor(
                HeaderInterceptor(
                    mapOf(
                        "Authorization" to "Bearer $token",
                        "User-Agent" to "MyAppClient/1.0"
                    )
                )
            )
            .addInterceptor(LogInterceptor(printBody = true))



        // 可以继续添加更多拦截器
    }

    /**
     * 关闭HttpClient释放资源
     */
    fun close() {
        HttpClientManager.close()


    }


    /**
     * =================== 对 HttpRequestHelper 的方法进行封装 ===================
     */

    suspend inline fun <reified T : Any> get(
        url: String,
        headers: Map<String, String>? = null,
        params: Map<String, String>? = null,
        isInfoResponse: Boolean = true
    ) = HttpRequestHelper.get<T>(url, headers, params, isInfoResponse)


    suspend inline fun <reified T : Any> postForm(
        url: String,
        headers: Map<String, String>? = null,
        params: Map<String, String>? = null,
        isInfoResponse: Boolean = true
    ) = HttpRequestHelper.postForm<T>(url, headers, params, isInfoResponse)


    suspend inline fun <reified T : Any> postJsonString(
        url: String,
        headers: Map<String, String>? = null,
        params: String? = null,
        isInfoResponse: Boolean = true
    ) = HttpRequestHelper.postJsonString<T>(url, headers, params, isInfoResponse)


    suspend inline fun <reified T : Any> put(
        url: String,
        headers: Map<String, String>? = null,
        params: Map<String, Any>? = null,
        type: Type,
        isInfoResponse: Boolean = true
    ) = HttpRequestHelper.put<T>(url, headers, params, isInfoResponse)


    suspend inline fun <reified T : Any> delete(
        url: String,
        headers: Map<String, String>? = null,
        isInfoResponse: Boolean = true
    ) = HttpRequestHelper.delete<T>(url, headers, isInfoResponse)


    suspend inline fun <reified T : Any> head(
        url: String,
        headers: Map<String, String>? = null,
        isInfoResponse: Boolean = true
    ) = HttpRequestHelper.head<T>(url, headers, isInfoResponse)


    suspend inline fun <reified T : Any> options(
        url: String,
        headers: Map<String, String>? = null,
        isInfoResponse: Boolean = true
    ) = HttpRequestHelper.options<T>(url, headers, isInfoResponse)


}
